<?php
	class ESQuery{
		const FUNC_IS_LOGGED = "ISLOGGED";
		const FUNC_LOGIN = "LOGIN";
		const FUNC_LOGOUT = "LOGOUT";
		const FUND_GET_DBS = "GETDBS";
		const FUNC_GET_TABLE_DATA = "GETTABLEDATA";
		const FUNC_GET_BASIC_DATA = "GETBASICDATA";

		private $link;
		private $response = array();
		private $logged = false;

		private function getLink($useSession=false,$Hostname="",$Username="",$Password="")
		{
			if($useSession)
			{
				global $_SESSION;
				$Hostname = $_SESSION["ESQHOSTNAME"];
				$Username = $_SESSION["ESQUSERNAME"];
				$Password = $_SESSION["ESQPASSWORD"];
			}
			if($this->link = @mysql_connect($Hostname,$Username,$Password))
			{
				return;
			}
			echo json_encode(array("Error"=>Debug::ERROR_NO_LINK));
			exit;
		}

		private function getDB($Database)
		{
			if(!mysql_select_db($Database,$this->link))
			{
				$this->response["Error"] = mysql_error($this->link);
				$this->printResponse();
			}
		}
		
		private function query($query)
		{
			if($result = @mysql_query($query,$this->link))
			{
				return $result;
			}
			else
			{
				$this->response["Error"] = mysql_error($this->link);
				$this->printResponse();
			}
		}
		
		private function htmlspecialchars($Data)
		{
			if(is_array($Data))
			{
				foreach($Data as $key => $value)
				{
					$Data[$key] = htmlspecialchars($value);
				}
			}
			elseif(is_string($Data))
			{
				$Data = htmlspecialchars($Data);
			}
			return $Data;
		}
		
		public function isLogged()
		{
			if($this->logged)
			{
				return;
			}

			global $_SESSION;
			if(isset($_SESSION["ESQLOGGED"]))
			{
				$this->logged = true;
				$this->response["Logged"] = true;
			}
			else
			{
				$this->response["Logged"] = false;
				$this->printResponse();
			}
		}
		
		public function login()
		{
			global $_REQUEST;
			global $_SESSION;

			$this->getLink(false,$_REQUEST["Hostname"],$_REQUEST["Username"],$_REQUEST["Password"]);

			$_SESSION["ESQLOGGED"] = true;
			$_SESSION["ESQHOSTNAME"] = $_REQUEST["Hostname"];
			$_SESSION["ESQUSERNAME"] = $_REQUEST["Username"];
			$_SESSION["ESQPASSWORD"] = $_REQUEST["Password"];
			$this->isLogged();
		}

		public function logout()
		{
			global $_SESSION;
			$_SESSION = array();
			$this->isLogged();
		}
		
		public function printResponse()
		{
			echo json_encode($this->response);
			exit;
		}
		
		public function execQuery()
		{
			$this->isLogged();
			
		}
		
		public function getDBS()
		{
			$this->isLogged();
			$this->getLink(true);
			
			$DBS = array();
			$Tables = array();
			$DBList = mysql_list_dbs($this->link);
			while($DBRow = mysql_fetch_object($DBList))
			{
				$DBS[] = $DBRow->Database;
				mysql_select_db($DBRow->Database,$this->link);
				$TBList = $this->query("SHOW TABLES FROM `{$DBRow->Database}`");
				$Tables[$DBRow->Database] = array();
				while($TBRow = mysql_fetch_array($TBList))
				{
					$Tables[$DBRow->Database][] = $TBRow[0];
				}
			}
			$this->response["DatabaseList"] = $DBS;
			$this->response["TableList"] = $Tables;
		}
		
		public function getTableFields($Database,$Table)
		{
			$this->isLogged();
			$this->getLink(true);
			$this->getDB($Database);
			
			$Fields = array();
			$result = $this->query("SHOW COLUsMNS FROM `{$Table}`");
			while($row = mysql_fetch_assoc($result))
			{
				$Fields[] = $row;
			}
			$this->response["Fields"] = $Fields;
		}
		
		public function getTableData($Database,$Table)
		{
			$this->isLogged();
			$this->getLink(true);
			$this->getDB($Database);
			
			$this->getTableFields($Database,$Table);
			
			$Data = array();
			$result = $this->query("SELECT * FROM `{$Table}` LIMIT 20");
			while($row = mysql_fetch_assoc($result))
			{
				$Data[] = $this->htmlspecialchars($row);
			}
			$this->response["Data"] = $Data;
		}

		public function getBasicData()
		{
			$this->isLogged();
			$this->getLink(true);
			
			$this->response["MySQLClient"] = mysql_get_client_info();
			$this->response["MySQLHost"] = mysql_get_host_info();
			$this->response["MySQLProtocol"] = mysql_get_proto_info();
			$this->response["MySQLServer"] = mysql_get_server_info();
			$this->getDBS();
		}
	}
?>